package com.jiandan.plugin

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.internal.dsl.DefaultConfig
import com.android.build.gradle.internal.dsl.SigningConfig
import org.gradle.api.GradleException
import org.gradle.api.Project

class PluginExtension {

    //360加固是否可用
    boolean firmEnable = true

    //build 之前收否clean
    boolean cleanBeforeBuildApk = true
    //关联assembleRelease
    boolean attachAssembleRelease = true

    //输出总路径 默认项目跟路径/360Firm
    String outPath
    //加固原文件 为null 是自动打包
    String apkFilePath

    //下载地址
    String firmDownloadUrl

    //360加固程序文件夹 提供以后不再手动下载
    String firmJarPath
    //app加固以后文件夹 名称
    String firmResultDirName = "firmResult"
    //360加固账号
    String firmAccountName
    //360加固密码
    String firmAccountPwd
    // 渠道文件路径 不传不打包多渠道
    String channelFilePath
    //app 名称
    String appName

    //apk签名文件路径
    String storeFilePath
    //apk签名文件密码
    String storePassword
    //apk签名文件别名
    String keyAlias
    //apk签名文件别名密码
    String keyPassword
    //apk versionCode
    int apkVersionCode
    //apk versionName
    String apkVersionName

    //-nocert 跳过签名校验
    //-analyse 选择数据分析服务
    //-x86 支持 x86 架构设备
    //-crashlog 选择崩溃日志服务
    //360加固高级选项 -x86 -data -crashlog -analyse -nocert
    String firm360Config = ""

    //上传蒲公英
    boolean pgyerEnable = true
    //上传蒲公英的key
    String pgyerApikey

    //上传蒲公英的渠道
    String buildChannel
    //移动打包的apk
    boolean moveReleaseApk = true


    //上传友盟
    boolean umengEnable = false
    String mappingPath = "/outputs/mapping/release/mapping.txt"
    String umengApikey
    String umengSeckey
    String umengAppkey

    String downloadUrlMac = "http://down.360safe.com/360Jiagu/360jiagubao_mac.zip"

    String downloadUrlLinux = "http://down.360safe.com/360Jiagu/360jiagubao_linux_64.zip"

    String downloadUrlWin = "http://down.360safe.com/360Jiagu/360jiagubao_windows_32.zip"

    //系统名字
    private String osName
    //执行 firmTask 导致的任务 非AssembleRelease
    private boolean isFirmTask = false

    private boolean apkIsBuild = false

    private String buildDir

    String getBuildDir() {
        return buildDir
    }
    String getOsName() {
        return osName
    }

    void setOsName(String osName) {
        this.osName = osName
    }

    boolean getIsFirmTask() {
        return isFirmTask
    }

    void setIsFirmTask(boolean isFirmTask) {
        this.isFirmTask = isFirmTask
    }

    boolean getApkIsBuild() {
        return apkIsBuild
    }

    void setApkIsBuild(boolean apkIsBuild) {
        this.apkIsBuild = apkIsBuild
    }

    static PluginExtension getConfig(Project project) {
        def config = project.getExtensions().findByType(PluginExtension.class)
        if (config == null) {
            throw new GradleException("打包配置为空")
        }
        if (config.outPath == null || config.outPath.length() == 0) {
            config.outPath = project.rootDir.absolutePath + File.separator + "360Firm"
        }
        def os = System.getProperty("os.name").toLowerCase()
        if (os.contains("linux")) {
            config.osName = "linux"
            if (config.firmDownloadUrl == null) {
                config.firmDownloadUrl = config.downloadUrlLinux
            }
        } else if (os.contains("mac")) {
            config.osName = "mac"
            if (config.firmDownloadUrl == null) {
                config.firmDownloadUrl = config.downloadUrlMac
            }
        } else {
            config.osName = "win"
            if (config.firmDownloadUrl == null) {
                config.firmDownloadUrl = config.downloadUrlWin
            }
        }
        if (config.firmJarPath == null) {
            config.firmJarPath = config.outPath + "/" + "360jiagubao" + config.getOsName()
        }
        return config
    }


    def initFirm() {
        if (firmEnable) {
            if (firmAccountName == null || firmAccountPwd == null) {
                throw new GradleException("360加固账号密码没有配置")
            }
            if (apkFilePath != null && (apkFilePath.length() == 0 || !new File(apkFilePath).exists())) {
                throw new GradleException("加固apk文件不存在 $apkFilePath")
            }
        }
    }

    def needBuildApk() {
        return apkFilePath == null && (firmEnable || pgyerEnable)
    }

    def initPgyer() {
        if (pgyerEnable) {
            if (pgyerApikey == null) {
                throw new GradleException("蒲公英apiKey没有配置")
            }
            if (apkFilePath != null && (apkFilePath.length() == 0 || !new File(apkFilePath).exists())) {
                throw new GradleException("蒲公英上传apk文件不存在 $apkFilePath")
            }
        }
    }

    def initUmeng() {
        if (umengEnable) {
            if (umengApikey == null) {
                throw new GradleException("友盟Apikey没有配置")
            }
            if (umengSeckey == null) {
                throw new GradleException("友盟Seckey没有配置")
            }
            if (umengAppkey == null) {
                throw new GradleException("友盟Appkey没有配置")
            }
        }
    }
    def initConfig(Project project) {
        buildDir = project.getBuildDir().getAbsolutePath()
        initSignConfig(project)
        initFirm()
        initPgyer()
        initUmeng()
    }

    def initSignConfig(Project project) {
        if (apkVersionName == null) {
            BaseExtension extension = project.extensions.getByName("android") as BaseExtension
            Collection<SigningConfig> signingConfigs = extension.getSigningConfigs()
            signingConfigs.forEach { signingConfig ->
                if (signingConfig.name == "release") {
                    this.storeFilePath = signingConfig.storeFile.absolutePath
                    this.keyAlias = signingConfig.keyAlias
                    this.storePassword = signingConfig.storePassword
                    this.keyPassword = signingConfig.keyPassword
                }
            }
            DefaultConfig defaultConfig = extension.getDefaultConfig()
            this.apkVersionName = defaultConfig.versionName
            this.apkVersionCode = defaultConfig.versionCode
        }
        if (firmEnable && (this.storeFilePath == null || this.keyAlias == null
                || this.storePassword == null || this.keyPassword == null)) {
            if (this.storeFilePath == null || this.keyAlias == null
                    || this.storePassword == null || this.keyPassword == null) {
                throw new GradleException("签名配置错误,需要配置签名\nstoreFilePath = $storeFilePath, keyAlias = $keyAlias, storePassword = $storePassword, keyPassword = $keyPassword")
            }
        }
    }

}
